﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Text;
using System.Web.UI.HtmlControls;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class XSDD_Query : BasePage
    {
        private int iStatus
        {
            get { return this.ViewState["iStatus"] == null ? 0 : Convert.ToInt32(this.ViewState["iStatus"]); }
            set { this.ViewState["iStatus"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            this.AspNetPager1.PageSize = GridView1.PageSize;
            BindDepartment(this.ddlDepartments, EnumDepartment.Marketing, "请选择");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            iStatus = Convert.ToInt32(e.CommandArgument);
            this.AspNetPager1.CurrentPageIndex = 1;
            GridView1.Columns[0].Visible = GridView1.Columns[11].Visible = iStatus == 0;
            BindData();
        }

        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("DDZT", this.iStatus.ToString());
            if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                dc.Add("BH", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(tbKHMC.Text.Trim()))
                dc.Add("KHMC", tbKHMC.Text.Trim());
            if (!string.IsNullOrEmpty(ddlDepartments.SelectedValue))
                dc.Add("Department", ddlDepartments.SelectedValue);
            if (!string.IsNullOrEmpty(hidStaff.Value))
                dc.Add("Staff", hidStaff.Value);
            if (!string.IsNullOrEmpty(tbDHSJ1.Text.Trim()))
                dc.Add("DHSJ1", tbDHSJ1.Text.Trim());
            if (!string.IsNullOrEmpty(tbDHSJ2.Text.Trim()))
                dc.Add("DHSJ2", tbDHSJ2.Text.Trim());
            if (!string.IsNullOrEmpty(tbCHSJ1.Text.Trim()))
                dc.Add("CHSJ1", tbCHSJ1.Text.Trim());
            if (!string.IsNullOrEmpty(tbCHSJ2.Text.Trim()))
                dc.Add("CHSJ2", tbCHSJ2.Text.Trim());
            using (XSDDBLL xBLL = new XSDDBLL())
            {
                int RecordCount = 0;
                List<vXSDD> itemLists = xBLL.GetXSDD(dc, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                vXSDD entity = e.Row.DataItem as vXSDD;
                ImageButton btnEdit = (ImageButton)(e.Row.FindControl("btnEdit"));
                ImageButton btnConfirm = (ImageButton)(e.Row.FindControl("btnConfirm"));
                Label lblZT = ((Label)(e.Row.FindControl("lblZT")));

                if (entity.DDZT == 0)
                {
                    lblZT.Text = "待确认";
                    lblZT.ForeColor = System.Drawing.Color.Red;
                    btnConfirm.Visible = true;
                    btnEdit.Visible = true;
                    btnEdit.PostBackUrl = string.Format("XSDD_Edit.aspx?id={0}", entity.ID);
               }
                else
                {
                    lblZT.ForeColor = System.Drawing.Color.Green;
                    lblZT.Text = "已确认";
                    btnConfirm.Visible = false;
                    btnEdit.Visible = false;
                }
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Guid id = Guid.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            int result;
            using (XSDDBLL xBLL = new XSDDBLL())
            {
                string bh = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.XSHT), out ywItem, out isAdd);
                result = xBLL.ConfirmOrder(id, bh);
            }
            if (result == 1)
            {
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
                ShowMessage("确认成功");
            }
            else if (result == -1)
                ShowMessage("确认失败。订单需求的产品中，现有产品库未建立。");
            else
                ShowMessage("确认失败");
            BindData();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (XSDDBLL xBLL = new XSDDBLL())
                {
                    if (xBLL.Deletes("ID", sbIds.ToString()))
                        this.ShowMessage("删除成功");
                    else
                        this.ShowMessage("删除失败");
                }
                BindData();
            }
        }
    }
}